//
//  MainTabBarController.m
//  美团外卖
//
//  Created by Alex on 16/9/23.
//  Copyright © 2016年 Alex. All rights reserved.
//

#import "MainTabBarController.h"
#import "MainNavigationController.h"

@interface MainTabBarController ()

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1. 实例化一个可变数组
    NSMutableArray *array = [NSMutableArray array];
    
    // 2. 添加子控制器
    [array addObject:[self addChildViewControllerWithClassName:@"LBHomeViewController" title:@"首页" imageName:@"v2_home"]];
    [array addObject:[self addChildViewControllerWithClassName:@"LBMarketViewController" title:@"闪电超市" imageName:@"v2_order"]];
    [array addObject:[self addChildViewControllerWithClassName:@"LBProfileViewController" title:@"我的" imageName:@"v2_my"]];

    // 3. 赋值
    self.viewControllers = array;

}

#pragma mark
#pragma mark - 实例化子控制器
- (MainNavigationController *)addChildViewControllerWithClassName:(NSString*)className title:(NSString *)title imageName:(NSString *)imageName{
    
    // 1. 把字符串转为class
    Class cls = NSClassFromString(className);
    
    // 1.1 断言判断类是正确的
    NSAssert([cls isSubclassOfClass:[UIViewController class]], @"类的名称不正确");
    
    // 2. 实例化控制器
    UIViewController *controller = [[cls alloc]init];
    
    // 3. 设置控制器的属性
    // 3.1 设置title
    // controller.navigationItem.title
    // controller.tabBarItem.title
    // 这种设置相当于上面两行代码的效果
    controller.title = title;
    
    // 3.2 设置普通状态的image
    controller.tabBarItem.image = [[UIImage imageNamed:imageName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 3.3 设置选中的图片
    controller.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_r",imageName]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 3.4 643205 设置选中状态的文字的颜色
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x643205)} forState:UIControlStateSelected];
    
    // 4. 实例化导航控制器
    MainNavigationController *nav = [[MainNavigationController alloc]initWithRootViewController:controller];
    
    return nav;
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
